from typing import Annotated

import uvicorn
from fastapi import FastAPI,Query
from fastapi.openapi.docs import get_swagger_ui_html, get_swagger_ui_oauth2_redirect_html, get_redoc_html
from starlette.staticfiles import StaticFiles
from pydantic import BaseModel

# 创建FastAPI实例，禁用掉FastAPI会自动生成Swagger UI和Redoc文档
app = FastAPI(docs_url=None, redoc_url=None)

# 静态文件挂载（路由与静态文件映射）
app.mount("/static", StaticFiles(directory="../statics"), name="static")


# 重新提供一个Swagger UI，相当于我们自己重写了这么一个路由
@app.get("/docs", include_in_schema=False)
def custom_swagger_ui():
    return get_swagger_ui_html(
        openapi_url = "/openapi.json",
        title = "Swagger UI for FastAPI",
        swagger_js_url="/static/swagger-ui-bundle.min.js",
        swagger_css_url="/static/swagger-ui.min.css"
    )
@app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
async def swagger_ui_redirect():
    return get_swagger_ui_oauth2_redirect_html()

# 这里与Swagger UI一样，重新提供Redoc
@app.get("/redoc", include_in_schema=False)
def custom_redoc():
    return get_redoc_html(
        openapi_url = "/openapi.json",
        title = "Redoc for FastAPI",
        redoc_js_url="/static/redoc.standalone.js"
    )


@app.get("/items")
async def read_items(q: Annotated[str | None, Query(max_length=50)] =  None):    # Annotated
# async def read_items(q: str | None = Query(default=None, max_length=50)):   # non-Annotated
    results = {
        "items":[
            {"item_name": "智能音箱"},
            {"item_name": "智能家居"},
            {"item_name": "智能设备"},
            {"item_name": "存储设备"},
        ]
    }
    if q:
        results.update({"q": q})
    return results

@app.get("/users")
# 要求查参数q为必选，并且参数值长度在5-20之间
async def read_users(q: Annotated[str, Query(max_length=20,min_length=5)]):
    results = {"users":[
        {"username": "tom"},
        {"username": "jerry"},
        {"username": "lucy"},
        {"username": "lily"},
    ]}
    results.update({"q": q})
    return results

@app.get("/orders")
async def read_orders(order_no: Annotated[str | None, Query(min_length=5,max_length=20,pattern="^SO\\d+$")] = None):
    return {"order_no": order_no}


@app.get("/emps")
async def read_emps(emp_no: list[str]):
    return {"emp_no": emp_no}


@app.get("/students")
async def read_students(student_no: Annotated[list[str], Query()]):
    return {"student_no": student_no}

@app.get("/goods")
async def read_goods(goods_no: Annotated[list[str], Query()] = ['5482324','234324']):
    return {"goods_no": goods_no}

@app.get("/teachers")
async def read_teachers(teacher_name: Annotated[str, Query(
    alias="teacher-name",                   # 别名
    title="teacher_title",                  # 标题,在OpenAPI文档中没有显示
    description="teacher_description",      # 描述
    min_length=1,                           # 最小长度
    max_length=10,                          # 最大长度
    regex="^[a-zA-Z0-9_-]{1,10}$",          # 正则表达式
    deprecated=True,                        # 弃用
    example="jack Ma",                      # 示例
)] = "jack"                                 # 默认值
                        ):
    return {"teacher_name": teacher_name}


if __name__ == '__main__':
    app.debug = True
    uvicorn.run("main:app", host="127.0.0.1", port=8004, reload=True)

